package io.gitee.sfch.chunmi.service.system.impl;


import com.mybatisflex.core.query.QueryChain;
import io.gitee.sfch.chunmi.Tables;
import io.gitee.sfch.chunmi.common.tree.BaseTreeNode;
import io.gitee.sfch.chunmi.common.tree.TreeBuilder;
import io.gitee.sfch.chunmi.dao.system.entity.SystemMenu;
import io.gitee.sfch.chunmi.dao.system.mapper.SystemMenuMapper;
import io.gitee.sfch.chunmi.service.system.SystemMenuService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SystemMenuServiceImpl implements SystemMenuService {

    @Resource
    private SystemMenuMapper systemMenuMapper;

    @Override
    public List<BaseTreeNode<SystemMenu>> findMenu(Long productId) {
        List<SystemMenu> menus = QueryChain.of(systemMenuMapper)
                .where(Tables.systemMenu.productId.eq(productId))
                .list();

        return new TreeBuilder<SystemMenu, BaseTreeNode<SystemMenu>>(SystemMenu::getId, SystemMenu::getParentId,
                BaseTreeNode::new)
                .setRootParentId(0L)
                .setNodeExtraSetter((node, data) -> {
                    node.setTitle(data.getMenuName());
                    node.setData(data);
                })
                .build(menus);
    }
}
